﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
using System.Linq;

namespace PraiseSystem.Models
{
    public class Praise
    {
        public int PraiseID { get; set; }
        public virtual User Owner { get; set; }
        public string OwnerID { get; set; }
        public virtual ICollection<Receiver> Receivers { get; set; }
        public string DoRight { get; set; }
        public DateTime PraiseDate { get; set; }
        public bool DeleteFlag{ get; set;}
    }

    public class PraiseMap : EntityTypeConfiguration<Praise>
    {
        public PraiseMap()
        {
            ToTable("tb_Praise");
            HasKey(p => p.PraiseID);
            Property(p => p.PraiseDate).IsRequired();
            Property(p => p.DeleteFlag).IsRequired();
            Property(p => p.PraiseID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        }
    }

    public class CommentRepository
    {
        private static PraiseDBContext db;
        static CommentRepository()
        {
            db = new PraiseDBContext();
        }

        /// <summary>
        /// Get the latest comments according to entrys number or all comments.
        ///
        /// </summary>
        public IEnumerable<Praise> GetPraises(bool allComments = false, int entry = 1)
        {
            return allComments ? db.Praises
            : db.Praises.Where(p => p.PraiseID > db.Praises.Max(n => n.PraiseID) - entry);
        }         
    }
}
